package com.dw.dwdzcommerce.view.mainContent.list;

import com.dw.dwdzcommerce.R;
import com.dw.dwdzcommerce.base.activity.BaseActivity;
import com.dw.dwdzcommerce.entity.AdPage;
import com.dw.dwdzcommerce.entity.MainContentEntity;
import com.dw.dwdzcommerce.entity.Shop;
import com.dw.dwdzcommerce.view.mainContent.SelectLayout;
import com.dw.dwdzcommerce.view.mainContent.ad.ADPagerLayout;
import com.dw.dwdzcommerce.view.mainContent.list.MainContentListLayout.OnContentClickListener;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

public class MainContentListAdapter extends BaseAdapter {

	private static final String TAG = "MainContentListAdapter";
	private Shop[] mShops;
	private AdPage[] mAdPages;
	private Context mContext;
	private SelectLayout mSelectLayout;
	private ADPagerLayout mPageLayout;

	public MainContentListAdapter(MainContentEntity mData, ADPagerLayout pagerLayout, SelectLayout mSelectLayout,
			Context mContext) {
		this.mContext = mContext;
		mAdPages = mData.getAdPages();
		mShops = mData.getShops();
		this.mSelectLayout = mSelectLayout;
		this.mPageLayout = pagerLayout;
	}

	public MainContentListAdapter(MainContentEntity mData, BaseActivity mContext) {
		ADPagerLayout pagerLayout = new ADPagerLayout();
		pagerLayout.initModule(mContext);
		SelectLayout mSelectLayout = new SelectLayout();
		mSelectLayout.initModule(mContext);

		mAdPages = mData.getAdPages();
		mShops = mData.getShops();
		this.mContext = mContext;
		this.mSelectLayout = mSelectLayout;
		this.mPageLayout = pagerLayout;

	}

	@Override
	public int getCount() {
		return mShops.length + 2;
	}

	@Override
	public Object getItem(int position) {
		if (position == 0) {
			return mAdPages;
		}
		if (position == 1) {

			return mSelectLayout.getView();
		}

		return mShops[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0) {
			convertView = mPageLayout.getADView();
		} else if (position == 1) {
			convertView = mSelectLayout.getView();
		} else {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_fragment_main_content_lis, null);
			MainContentListLayout mListLayout = new MainContentListLayout();
			mListLayout.setmListener(new OnContentClickListener() {

				@Override
				public void onFocus(View view, int mShopId) {
					Toast.makeText(mContext, "" + view.getTag() + " mShopId=" + mShopId, Toast.LENGTH_SHORT).show();

				}

				@Override
				public void onLocClick(View view, int mShopId) {
					Toast.makeText(mContext, " mShopId=" + mShopId, Toast.LENGTH_SHORT).show();
				}
			});
			position -= 2;
			mListLayout.initModule(convertView, (FragmentActivity) mContext);
			mListLayout.setShopLoc(mShops[position].getAddress());
			mListLayout.setShopName(mShops[position].getShopName());
			mListLayout.setShopRule(mShops[position].getActRules());
			mListLayout.setShopAvator(mShops[position].getShopAvator(), mContext);
			mListLayout.setShopContent(mShops[position].getImages());
			mListLayout.setmShopId(mShops[position].getShopId());
			mListLayout.setPhone(mShops[position].getPhone());
		}
		return convertView;
	}


}
